import express from "express";
import dotenv from "dotenv";
dotenv.config();

import cors from "cors";
import "express-async-errors";
// require("express-async-errors");

import { logger, logRequestResponse } from "@/comm/logger"

const app = express();
const PORT = process.env.PORT ?? 8000;

app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.get('/',(req,res)=>res.send('ok'))
app.get('/a',(req,res)=>{throw new Error('error')})

// 静态资源代理
//app.use(express.static("public"));

app.use(logRequestResponse);

// 错误处理器中间件
app.use((err, req, res, next) => {
    // 设置响应的结构
    res.status(err.status || 500);
    res.send(err.message);
    logger.error(`[异常]:${err.message}\n${err.stack}`)
});

app.listen(PORT, () => {
    console.log(`Server is running on port http://127.0.0.1:${PORT}`);
});

export default app;
